define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prl/viewsfields/index',
                    add_url: 'prl/viewsfields/add',
                    edit_url: 'prl/viewsfields/edit',
                    del_url: 'prl/viewsfields/del',
                    multi_url: 'prl/viewsfields/multi',
                    table: 'prl_viewsfields',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'public', title: __('Public'), searchList: {"0":__('Public 0'),"1":__('Public 1')}, formatter: Table.api.formatter.normal},
                        {field: 'type', title: __('Type')},
                        {field: 'name', title: __('Name')},
                        {field: 'title', title: __('Title')},
                        {field: 'tip', title: __('Tip')},
                        {field: 'common', title: __('Common'), searchList: {"0":__('Common 0'),"1":__('Common 1')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams : function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if(Config.ids != null && Config.view_id != null ){
                        filter.id = Config.ids;
                        op.id = "in";
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $(document).on("change", "form select[name='row[public]']", function (e) {
                    var value = $(this).val();
                    $("#c-views_ids").parents('.form-group').toggleClass("hide", value == '0' ? true : false);
                });

                $(document).on("change", "form select[name='row[type]']", function (e) {
                    var value = $(this).val();
                    $("#c-content").parents('.form-group')
                    .toggleClass("hide", ['select', 'selects', 'checkbox', 'radio'].indexOf(value) > -1  ? false : true);
                });

                //不可见的元素不验证
                $("form").data("validator-options", {ignore: ':hidden'});
                Form.api.bindevent($("form[role=form]"));

                //给input设置一个宽度 否则显示的宽度150px
                $(".selectpage").css('width', $(".selectpage").parent('div').width());
                //触发选中
                $("form select[name='row[public]']").trigger("change");
                $("form select[name='row[type]']").trigger("change");
                
            }
        }
    };
    return Controller;
});